<!--
 @description 客户跟进
 @fileName index.vue
 @author zengqiongying
 @created 2022/11/06 14:20:21
-->
<template>
  <div class="page-has-bottom bg_gray">
    <ComHeader title="跟进" bg-color="#FAFBFE" />
    <div class="cus-fixed-bottom">
      <van-button type="primary" block round class="cus-btn-submit" :disabled="!enbleSubmit" @click="handlerConfirm">确定</van-button>
    </div>
    <van-form :show-error-message="false" @submit="handlerConfirm">
      <!--
        跟进类型支取：
          ArrivalForRuler 上门获尺,
          ApptArrival 预约到店,
          CustomerInShop 顾客进店,
          ContactPhoneOrWx 电话(微信)联系
       -->
      <FieldPicker
        v-model="formData.followUpType"
        title="跟进类型"
        placeholder="请选择(必选)"
        :url="`/data-dict/app/v2/dictionary/CRM_FOLLOW_UP_TYPE`"
        label-name="dictName"
        value-name="dictCode"
        method="get"
        :params="{ query: { appCode: 'top2-crm', domainCode: 'marketing' }, type: 0 }"
        required
        :includes="['ArrivalForRuler','ApptArrival','CustomerInShop','ContactPhoneOrWx']"
      />
      <Cell
        v-model="formData.followUpContent"
        title="跟进内容"
        placeholder="请填写"
        :max-length="200"
        :right="false"
        is-input
        required
      />
      <FieldDateTime
        v-model="formData.nextFollowupTime"
        title="下次跟进时间"
        placeholder="请选择"
        type="datetime"
        :min-date="measuredMinDate"
        clear
      />
      <Cell title="上传附件" is-column custom :right="false">
        <AttachmentUploader
          ref="files"
          v-model="formData.files"
          module-type="FollowUpFile"
          multiple
          :object-id="nextId"
        />
      </Cell>

      <Cell title="语音备注" custom :right="false">
        <FieldAudio
          v-model="formData.voiceRemark"
          :length="30"
          :object-id="nextId"
          module-type="FollowUpVoice"
          title="语音备注"
        />
      </Cell>
    </van-form>
  </div>
</template>

<script>
import FieldPicker from '@/components/FieldPicker'
import FieldAudio from '@/components/FieldAudio'
import FieldDateTime from '@/components/FieldDateTime'
import AttachmentUploader from '@/components/AttachmentUploader'
import Cell from '@/components/ListCell'
import { bus } from '@/utils/bus'
import { getId } from '@/apis/common'
import { customerFollowUp } from '@/apis/customer'
export default {
  name: 'CustomerFollowUp',
  components: { Cell, FieldPicker, FieldAudio, AttachmentUploader, FieldDateTime },
  filters: {},
  mixins: [],
  data() {
    return {
      objectId: this.$route.query.id, // 当前跟进对象Id
      objectTypeDisplay: this.$route.query.name || '', // 当前跟进对象名称
      type: this.$route.query.type,
      nextId: '',
      measuredMinDate: new Date(),
      formData: {
        followUpType: '',
        followUpContent: '',
        nextFollowupTime: '',
        files: [],
        voiceRemark: ''

      }
    }
  },

  computed: {
    enbleSubmit() {
      return this.formData.followUpType && this.formData.followUpContent
    },
    objectType() {
      // 跟进对象类型： 客户 ClueCustomer ， 商机 Opty
      const type = this.$route.params.type
      if (type === 'opty') {
        return 'Opty' // 商机
      } else if (type === 'customer') {
        return 'ClueCustomer' // 客户
      } else {
        return ''
      }
    }
  },

  created() {
    getId().then(res => {
      this.nextId = res.data
    })
  },
  activated() {},

  mounted() {},

  methods: {
    handlerConfirm() {
      if (!this.formData.followUpType) {
        this.$toast('请选择跟进类型')
        return
      }
      if (!this.formData.followUpContent) {
        this.$toast('请填写跟进内容')
        return
      }
      const data = {
        id: this.nextId,
        objectId: this.objectId,
        objectTypeDisplay: this.objectTypeDisplay,
        objectType: this.objectType,
        ...this.formData
      }
      customerFollowUp(data).then(res => {
        this.$toast('操作成功')
        bus.$emit('refreshCustomerItem', {
          title: '跟进',
          type: 'follow',
          id: this.objectId
        })
        if (this.objectType === 'opty') {
          bus.$emit('refreshOptyItem', {
            title: '跟进',
            type: 'follow',
            id: this.objectId
          })
        }
        this.$router.go(-1)
      })
    }
  }
}

</script>
<style lang='less' scoped>

</style>
